package com.fan.shop.businesslogic;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.fan.shop.domain.PurchaseOrder;
import com.fan.shop.domain.Stock;
import com.fan.shop.domain.StockReq;
import com.fan.shop.global.ShopGlobalVariables;
import com.fan.shop.model.userclass.MessageUserClass;
import com.fan.shop.process.ProcExec;
import com.fan.shop.services.PurchaseOrderService;
import com.fan.shop.services.ShopGlobalService;
import com.fan.shop.services.StockReqService;
import com.fan.shop.services.StockService;

public class MessageRetriveProc extends ProcExec<Integer> {

	private static Logger logger = Logger.getLogger(MessageRetriveProc.class);

	private List<MessageUserClass> stockMessagesList;
	private List<MessageUserClass> poMessagesList;
	private List<MessageUserClass> streqMessageList;

	private StockService stockService;
	private PurchaseOrderService purchaseOrderService;
	private StockReqService stockReqService;

	private ShopGlobalService shopGlobalService;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		MessageRetriveProc.logger = logger;
	}


	public List<MessageUserClass> getStockMessagesList() {
		return stockMessagesList;
	}

	public void setStockMessagesList(List<MessageUserClass> stockMessagesList) {
		this.stockMessagesList = stockMessagesList;
	}

	public List<MessageUserClass> getPoMessagesList() {
		return poMessagesList;
	}

	public void setPoMessagesList(List<MessageUserClass> poMessagesList) {
		this.poMessagesList = poMessagesList;
	}

	public List<MessageUserClass> getStreqMessageList() {
		return streqMessageList;
	}

	public void setStreqMessageList(List<MessageUserClass> streqMessageList) {
		this.streqMessageList = streqMessageList;
	}

	public StockService getStockService() {
		return stockService;
	}

	public void setStockService(StockService stockService) {
		this.stockService = stockService;
	}

	public PurchaseOrderService getPurchaseOrderService() {
		return purchaseOrderService;
	}

	public void setPurchaseOrderService(
			PurchaseOrderService purchaseOrderService) {
		this.purchaseOrderService = purchaseOrderService;
	}


	public StockReqService getStockReqService() {
		return stockReqService;
	}

	public void setStockReqService(StockReqService stockReqService) {
		this.stockReqService = stockReqService;
	}


	public ShopGlobalService getShopGlobalService() {
		return shopGlobalService;
	}

	public void setShopGlobalService(ShopGlobalService shopGlobalService) {
		this.shopGlobalService = shopGlobalService;
	}

	public Integer getStocksMessage() {

		if (stockMessagesList == null) {
			stockMessagesList = new ArrayList<MessageUserClass>();
		} else {
			stockMessagesList.clear();
		}
		for (Stock stock : stockService.getAllStocks()) {
			int threshold = Integer.parseInt(shopGlobalService.getShopGlobal(
					ShopGlobalVariables.LOW_STOCK_THRESHOLD).getValue());
			if (stock.getQuantity() < threshold) {
				MessageUserClass messageUserClass = new MessageUserClass();
				messageUserClass
						.setMessageType(ShopGlobalVariables.STOCK_MESSAGE_HEADER);
				messageUserClass.setTime(new Date(System.currentTimeMillis()));
				messageUserClass.setStatus(ShopGlobalVariables.MESSAGE_PENDING);
				messageUserClass
						.setMessageLink(ShopGlobalVariables.STOCKS_LINK);
				String message = stock.getId().getItemCode() + " in Store "
						+ stock.getId().getStoreCode()
						+ " doesn't have enough stock left!";
				messageUserClass.setMessage(message);

				stockMessagesList.add(messageUserClass);
			}
		}

		return ShopGlobalVariables.SUCCESS;
	}

	public Integer getPOsMessage() {

		if (poMessagesList == null) {
			poMessagesList = new ArrayList<MessageUserClass>();
		} else {
			poMessagesList.clear();
		}
		int enteringPOsCount = 0;
		int completedPOsCount = 0;
		for (PurchaseOrder purchaseOrder : purchaseOrderService
				.getAllPurchaseOrders()) {
			if (purchaseOrder.getStatus().equals(ShopGlobalVariables.ENTERING)) {
				enteringPOsCount++;
			}
			if (purchaseOrder.getStatus().equals(ShopGlobalVariables.COMPLETED)) {
				completedPOsCount++;
			}
		}
		String message = "Currently " + enteringPOsCount
				+ " purchase orders are in Entering status " + "and "
				+ completedPOsCount
				+ " purchase orders are in Completed status.";
		
		MessageUserClass messageUserClass = new MessageUserClass();
		messageUserClass.setMessageType(ShopGlobalVariables.PO_MESSAGE_HEADER);
		messageUserClass.setMessage(message);
		messageUserClass.setMessageLink(ShopGlobalVariables.PURCHASE_ORDERS_LINK);
		messageUserClass.setTime(new Date(System.currentTimeMillis()));
		messageUserClass.setStatus(ShopGlobalVariables.MESSAGE_PENDING);
		
		poMessagesList.add(messageUserClass);
		
		return ShopGlobalVariables.SUCCESS;
	}
	
	
	public Integer getStReqsMessage() {

		if (streqMessageList == null) {
			streqMessageList = new ArrayList<MessageUserClass>();
		} else {
			streqMessageList.clear();
		}
		int enteringPOsCount = 0;
		int postedPOsCount = 0;
		for (StockReq stockReq : stockReqService.getAllStockReqs()) {
			if (stockReq.getStatus().equals(ShopGlobalVariables.ENTERING)) {
				enteringPOsCount++;
			}
			if (stockReq.getStatus().equals(ShopGlobalVariables.POSTED)) {
				postedPOsCount++;
			}
		}
		String message = "Currently " + enteringPOsCount
				+ " Stock Requisitions are in Entering status " + "and "
				+ postedPOsCount
				+ " Stock Requisitions are in Completed status.";
		
		MessageUserClass messageUserClass = new MessageUserClass();
		messageUserClass.setMessageType(ShopGlobalVariables.STREQ_MESSAGE_HEADER);
		messageUserClass.setMessage(message);
		messageUserClass.setMessageLink(ShopGlobalVariables.STOCKS_LINK);
		messageUserClass.setTime(new Date(System.currentTimeMillis()));
		messageUserClass.setStatus(ShopGlobalVariables.MESSAGE_PENDING);
		
		streqMessageList.add(messageUserClass);
		
		return ShopGlobalVariables.SUCCESS;
	}
	
	
	
	
	
	

	@Override
	public Integer processImpl() {
		int result = getStocksMessage();
		if (result == ShopGlobalVariables.FAILED) {
			return ShopGlobalVariables.FAILED;
		}
		result = getPOsMessage();
		if (result == ShopGlobalVariables.FAILED) {
			return ShopGlobalVariables.FAILED;
		}

		return ShopGlobalVariables.SUCCESS;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
